<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <script>
    let s1 = "some text";
    let s2 = s1.substring(2);
    console.log(s2);//me text;
    //Object工厂函数
    let obj = new Object("some string");
    console.log(obj instanceof String);//true
    //Boolean包装类型永远不建议使用.
    //Number
    let num = 10;
    console.log(num.toFixed(2));//10.00
    // 四舍五入toFixed
    let num1 = 10.005
    console.log(num1.toFixed(2));//10.01
    //toExponential()
    let num2 = 100;
    console.log(num2.toExponential());//1e+2
    //toPrecision()
    let num3 = 99;
    console.log(num3.toPrecision(1)); //1e+2
    console.log(num3.toPrecision(2)); //99
    console.log(num3.toPrecision(3)); //99.0
    console.log(Number.isInteger(99.00));//true
    console.log(Number.isSafeInteger(-1 * (2 ** 53) + 1));//true2
    //String
    let stringValue = "Sloan Li";
    //lenght:多少位16位码元
    console.log(stringValue.length);//8
    // charAt() : 返回给定索引位置的字符.
    console.log(stringValue.charAt(2));//o
    //charCodeAt():查看指定码元的字符编码.
    console.log(stringValue.charCodeAt(3));//97,a的字符编码为97.
    console.log(99 === 0x63);//true
    //fromCharCode()
    console.log(String.fromCharCode(0x61, 0x62, 0x63, 0x64, 0x65));//abcde
    console.log(String.fromCharCode(97, 98, 99, 100, 101));//abcde
    //codePointAt();
    let message = "ab😊de";
    console.log(String.fromCharCode(97, 98, 55357, 56842, 100, 101));
    console.log(String.fromCodePoint(0x1F60A));//😊
    console.log([..."ab😊de"]);
    console.log(String.fromCodePoint(97, 98, 128522, 100, 101));
    //有些字符既可以通过一个BMP字符表示,也可以通过一个代理对表示.
    console.log(String.fromCharCode(0x00C5));
    console.log(String.fromCharCode(0x212B)); //BMP码元表示
    console.log(String.fromCharCode(0x0041, 0x030A)); //代理对表示
    // concat()
    let s3 = "Sloan";
    let s4 = "Li";
    let s5 = s3.concat(s4);
    console.log(s5);//SloanLi
    //slice(),substr(),substring()
    let stringVal = "hello world";
    console.log(stringVal.slice(3));//lo world
    console.log(stringVal.substring(3));//lo world
    console.log(stringVal.substr(3));//lo world
    console.log(stringVal.slice(3, 7));//lo w(左闭右开);
    console.log(stringVal.substring(3, 7));//lo w
    console.log(stringVal.substr(3, 7));//lo worl(从索引3开始,提取7个字符)

    //slice(-3):负参数+字符长度 = -3+11=8
    console.log(stringVal.slice(-3));//rld
    //substring(-3): 负参数转换为0
    console.log(stringVal.substring(-3));//hello world
    // 负参数 + 字符长度 = -3+11=8
    console.log(stringVal.substr(-3));//rld
    //第二个负参数,转换为8
    console.log(stringVal.slice(3, -4));//lo w
    //第二个负参数,转换为0
    console.log(stringVal.substring(3, -4));//hel
    //第二个负参数,转换为0
    console.log(stringVal.substr(3, -4));//""

    let str = "Hello World";
    console.log(str.indexOf("o"));//4
    console.log(str.lastIndexOf("o"));//7
    console.log(str.indexOf("o", 6));//7
    console.log(str.lastIndexOf("o", 6));//4

    //查找字符串中所有的目标子字符位置(查找字母的所有位置)
    let stringValue0 = "Lorem ipis emd aos, se eomkx sfndkw e docs we";
    let positions = [];
    let pos = stringValue0.indexOf("e");
    while (pos > -1) {
      positions.push(pos);
      pos = stringValue0.indexOf("e", pos + 1);
    }
    console.log(positions);//[3, 11, 21, 23, 36, 44]

    //startsWith,endsWith,includes
    let message1 = "foobarbaz";
    console.log(message1.startsWith("foo"));//true
    console.log(message1.endsWith("baz"));//true
    console.log(message1.includes("bar"));//true
    console.log(message1.startsWith("foo", 1));//false
    console.log(message1.includes("bar", 4));//false
    console.log(message1.endsWith("bar"));//false
    console.log(message1.endsWith("bar", 6));//true

    let message2 = "    Sloan Li   ";
    let trimMessage2 = message2.trim();
    console.log(trimMessage2);

    let na = "na ";
    let repratNa = na.repeat(7);
    console.log(repratNa);//na na na na na na na

    let padString = "foo";
    console.log(padString.padStart(6));
    console.log(padString.padEnd(9, "."));//foo......
    console.log(padString.padStart(8, "bar"));//barbafoo
    console.log(padString.padEnd(8, "bar"));//foobarba

    let message3 = "abc";
    let stringIterator = message3[Symbol.iterator]();
    // 手动迭代.
    console.log(stringIterator.next());
    console.log(stringIterator.next());
    console.log(stringIterator.next()); //{value:"c",done:false}
    console.log(stringIterator.next()); //{value:undefined,done:true}
    for (const c of "abcde") {
      console.log(c);
    }
    console.log([...message3]);//["a","b","c"];
    console.log(message3.toUpperCase());

    //字符串模式匹配
    let text = "cat,bat,sat,fat";
    let pattern = /.at/;
    //pattern.exec(text)
    let matches = text.match(pattern);
    console.log(matches.index);//0
    console.log(matches[0]);//cat
    console.log(pattern.lastIndex);//0

    let pos1 = text.search(/at/);
    console.log(pos1);//1

    let result = text.replace("at", "ond");
    console.log(result);//只有cat被替换成了cond.
    result = text.replace(/at/g, "ond");
    console.log(result);//全部被替换.
    result = text.replace(/(.at)/g, "word($1)")
    console.log(result);//word(cat),word(bat),word(sat),word(fat)
    //将一些字符替换为对应的字符实体.
    function htmlEscape(text) {
      return text.replace(/[<>"&]/g, function (match, pos, originalText) {
        switch (match) {
          case "<":
            return "&lt";
          case ">":
            return "&gt";
          case "&":
            return "&amp";
          case "\"":
            return "&quot"
        }
      });
    }
    console.log(htmlEscape("<p class=\"gretting\">Hello World!</p>"));

    let colorText = "red,blue,green,yellow";
    let color1 = colorText.split(",");
    console.log(color1);//['red', 'blue', 'green', 'yellow']
    let color2 = colorText.split(",", 2);
    console.log(color2);//['red', 'blue']

    //localeCompare():
    let stringVal1 = "yellow"
    function determineOrder(value) {
      let result = stringVal1.localeCompare(value);
      if (result < 0) {
        console.log(`The string 'yellow' comes before the string '${value}'.`);
      } else if (result > 0) {
        console.log(`The string 'yellow' comes after the string '${value}'.`);
      } else {
        console.log(`The string 'yellow' is equal to the string '${value}'.`);
      }
    }
    determineOrder("brick");
    determineOrder("yellow");
    determineOrder("zoo");

    let uri = "http://www.baidu.com";
    console.log(encodeURI(uri));//http://www.baidu.com
    console.log(encodeURIComponent(uri));//http%3A%2F%2Fwww.baidu.com
    let uri1 = "http%3A%2F%2Fwww.baidu%20.com"
    console.log(decodeURI(uri1));//http%3A%2F%2Fwww.baidu.com
    console.log(decodeURIComponent(uri1));//http://www.baidu.com

    eval("console.log('hi')");//hi

    var color = "red";
    function sayColor() {
      console.log(window.color)
    }
    window.sayColor();//red
    // 没有明确指定this,this就是Global对象.
    let global = function () {
      return this;
    }();

    let values = [1, 2, 3, 4, 5, 6, 7];
    let max = Math.max(...values);//利用展开运算符可以求数组的最大最小值.
    console.log(max)//7

    //从1~10随机选择一个数:
    let n1 = Math.floor(Math.random() * 10 + 1);
    //从2~10随机选择一个数:
    let n2 = Math.floor(Math.random() * 9 + 2);
    //用函数计算可选的数
    function selectFrom(lowerNum, upperNum) {
      let choices = upperNum - lowerNum + 1;
      return Math.floor(Math.random() * choices + lowerNum)
    }
    // 3~10随机数
    console.log(selectFrom(3, 10));//
  </script>
</body>

</html>